package com.altran.mirth.migration.v211;

import java.util.ArrayList;

/**
 * Project :  Mirth
 * Author :   Altran
 *
 * Copyright (C) 2011 - Altran 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class UserFunction extends Occurency {

	ArrayList<String> listOfPattern;
	String replacePattern;
	String newFunction;

	ArrayList<Pattern> listOfPatterns;
	
	public static ArrayList<UserFunction> loadAllFunctions(){
		ArrayList<UserFunction> listOfFunctions = new ArrayList<UserFunction>();

		boolean defaultReplacable = true;
		
		// ********************************************* //
		UserFunction func = new UserFunction();
		func.setID(1);
		func.setName("ConnecToDB LOPR01");
		func.setNewFunction("ConnecToDB_LOPR01();");
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@lopr01_ip.ffe.healthnet.lu:1541:LOPR01','logimen','logimen');", func.getNewFunction(), defaultReplacable);
		//func.addPattern("jdbc:oracle:thin:@lopr01_ip.ffe.healthnet.lu:1541:LOPR01", func.getNewFunction(), false);

		listOfFunctions.add(func);
		
		// ********************************************* //
		func = new UserFunction();
		func.setID(2);
		func.setName("ConnecToDB KIAR04");
		func.setNewFunction("ConnecToDB_KIAR04();");
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@kiar04_ip.ffe.healthnet.lu:1565:kiar04','kissmed','k04fao');", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@kiar04-ip.ffe.healthnet.lu:1565:KIAR04','kissmed','k04fao');", func.getNewFunction(), defaultReplacable);
		func.addPattern("jdbc:oracle:thin:@kiar04_ip.ffe.healthnet.lu:1565:kiar04", func.getNewFunction(), false);
		
		listOfFunctions.add(func);		
		
		// ********************************************* //
		func = new UserFunction();
		func.setID(3);
		func.setName("ConnecToDB KIPR04");
		func.setNewFunction("ConnecToDB_KIPR04();");
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@kipr04_ip.ffe.healthnet.lu:1525:kipr04','kissmed','k04fao');", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.OracleDriver','jdbc:oracle:thin:@kipr04_ip.ffe.healthnet.lu:1525:KIPR04','Kissmed','k04fao');", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection(globalMap.get('insdft_driver'), globalMap.get('insdft_address'), globalMap.get('insdft_username'), globalMap.get('insdft_password'));", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection(globalMap.get('lxfdft_driver'), globalMap.get('lxfdft_address'), globalMap.get('lxfdft_username'), globalMap.get('lxfdft_password'));", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection(globalMap.get('lxfdft_driver'),globalMap.get('lxfdft_address'),globalMap.get('lxfdft_username'),globalMap.get('lxfdft_password'));", func.getNewFunction(), defaultReplacable);
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection(globalMap.get('driver'), globalMap.get('address'), globalMap.get('username'), globalMap.get('password'));", func.getNewFunction(), defaultReplacable);

		listOfFunctions.add(func);
		
		// ********************************************* //
		func = new UserFunction();
		func.setID(4);
		func.setName("ConnecToDB MIRTHDB");
		func.setNewFunction("ConnecToDB_MIRTHDB();");
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection('org.postgresql.Driver','jdbc:postgresql://localhost:5432/mirthdb','postgres','admin');", func.getNewFunction(), defaultReplacable);
		func.addPattern("jdbc:postgresql://localhost:5432/mirthdb", func.getNewFunction(), false);

		listOfFunctions.add(func);

		// ********************************************* //
		func = new UserFunction();
		func.setID(5);
		func.setName("ConnecToDB OTHER");
		func.setNewFunction("ConnecToDB(db_Name);");
		func.addPattern("DatabaseConnectionFactory.createDatabaseConnection", func.getNewFunction(), false);

		listOfFunctions.add(func);

		// ********************************************* //
		return listOfFunctions;
	}
	
	public String getResults(){
		int nbReplacement = 0;
		int nbToReplace = 0;
		for ( Pattern pattern : listOfPatterns){
			if ( pattern.isReplaceable() ) 
				nbReplacement += pattern.getNumberOfOccurency();
			else
				nbToReplace += pattern.getNumberOfOccurency();
		}
		return "	#"+this.getID()+" - "+this.getName()+"	Replaced : "+nbReplacement+"	To be Replaced : "+nbToReplace;
	}
	
	public ArrayList<String> getMatchingPatterns(){
		return listOfPattern;
	}
	
	/**
	 * @return the replacePattern
	 */
	public String getReplacePattern() {
		return replacePattern;
	}
	/**
	 * @param replacePattern the replacePattern to set
	 */
	public void setReplacePattern(String replacePattern) {
		this.replacePattern = replacePattern;
	}
	/**
	 * @return the newFunction
	 */
	public String getNewFunction() {
		return newFunction;
	}
	/**
	 * @param newFunction the newFunction to set
	 */
	public void setNewFunction(String newFunction) {
		this.newFunction = newFunction;
	}
	
	public void addReplacePattern(String pattern){
		if ( listOfPattern == null ) 
			listOfPattern = new ArrayList<String>();
			
		listOfPattern.add(pattern);
	}
	
	public void addPattern(Pattern pattern){
		if ( listOfPatterns == null )
			listOfPatterns = new ArrayList<Pattern>();
	
		listOfPatterns.add(pattern);
	}
	
	
	/**
	 * @return the listOfPatterns
	 */
	public ArrayList<Pattern> getListOfPatterns() {
		return listOfPatterns;
	}
	/**
	 * @param listOfPatterns the listOfPatterns to set
	 */
	public void setListOfPatterns(ArrayList<Pattern> listOfPatterns) {
		this.listOfPatterns = listOfPatterns;
	}

	public void addPattern(String oldPattern, String newPattern, boolean replaceable){
		if ( listOfPatterns == null )
			listOfPatterns = new ArrayList<Pattern>();
		
		Pattern p = new Pattern();
		p.setOldPattern(oldPattern);
		p.setNewPattern(newPattern);
		p.setReplaceable(replaceable);
		
		listOfPatterns.add(p);
	}
		
}
